package com.wenhai.exercise;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class StreamMap {
    public static void main(String[] args) {
        User user1 = new User("zhangsan", 10,20);
        User user2 = new User("zhangsan", 10,20);
        User user3 = new User("lisi", 30,20);
        List<User> list = new ArrayList<User>();
        list.add(user1);
        list.add(user2);
        list.add(user3);
        Map<String, List<User>> collect = list.stream().collect(Collectors.groupingBy(StreamMap::fetchGroupKey));
        //{zhangsan#beijing=[User{age=10, name='zhangsan', address='beijing'}, User{age=20, name='zhangsan', address='beijing'}],
        // lisi#shanghai=[User{age=30, name='lisi', address='shanghai'}]}
        System.out.println(collect);
    }
    private static String fetchGroupKey(User user){
        return user.getName() +"#"+ user.getPrice();
    }
}
